extends TextureRect

var tileType:Array
onready var kuang = $Kuang
onready var line = $RoutLine
var PerTexture = preload("res://map/PerTexture.tscn")
var mapTypes = {}
var perTiles = {}
var maxX = 0
var maxY = 0

func initMap(config:Dictionary):
	var configImage = config.get("image")
	var configSize = config.get("size")
	var configTileType = config.get("tileType")
	
	## 初始化图片
	var imageTexture = ImageTexture.new()
	imageTexture.load(configImage)
	texture = imageTexture
	
	## 初始化瓷砖
	tileInit(configTileType)
	
	pass

func _input(event):
	if event as InputEventMouseButton && event.button_index==BUTTON_LEFT && event.is_pressed():
		kuang.global_position = Util.getIndexByPosition(get_global_mouse_position()) * 32

#func tileInit(configTileType):
#	for x in range(configTileType.size()):
#		perTiles.append([])
#		for y in range(configTileType[x].size()):
#			var perTexture = PerTexture.instance()
#			perTexture.init(x, y , configTileType[y][x]) 
#			perTexture.position = Vector2(x*32, y*32)
#			perTiles[x].append(perTexture)
#			add_child(perTexture)
func tileInit(configTileType):
	maxX = configTileType.size()
	maxY = configTileType[0].size()
	for x in range(configTileType.size()):
		for y in range(configTileType[x].size()):
			var perTexture = PerTexture.instance()
			perTexture.init(y, x , configTileType[x][y]) 
			perTexture.position = Vector2(y*32, x*32)
			mapTypes[Vector2(y,x)] = configTileType[x][y]
			perTiles[Vector2(y,x)] = perTexture
			add_child(perTexture)
#
#	for p in perTiles.keys():
#		perTiles[Vector2(p.x, p.y)].showSpend()

func canWalkChange(tails):
	for point in tails:
		perTiles[point.toVector()].showWalkable()

func clearWalk():
	for p in perTiles.keys():
		perTiles.get(p).clearStat()
